<?php
class GradeAction extends AppAction {
	private $adminGrand;
	function __construct() {
		parent::__construct ();
		AdminVisitor::checkLogin ();
		$this->adminGrand = AdminSession::getAdminSessionInfo ();
	}
	/**
	 * 列表
	 */
	public function campgradelist() {
		AdminVisitor::checkGrant ( 'camp', FALSE );
		require_once APP_LIB_PATH . 'campLib.php';
		$campLib = new CampLib ();
		$campGrand = AdminVisitor::getAdminGrant ();
		if (! isset ( $campGrand ['camp'] )) {
			$campGrand = array ();
		} else {
			$campGrand = $campGrand ['camp'];
		}
		$summerGrade = $campLib->getGradeList ( 1 );
		$winterGrade = $campLib->getGradeList ( 2 );
		
		if (AdminVisitor::checkGrant ( 'manage', FALSE, FALSE )) {
			ViewData::$form_data->summerGrade = $summerGrade;
			ViewData::$form_data->winterGrade = $winterGrade;
		} else {
			ViewData::$form_data->summerGrade = $this->getAdminGradeInfo ( $summerGrade, $campGrand );
			ViewData::$form_data->winterGrade = $this->getAdminGradeInfo ( $winterGrade, $campGrand );
		}
		
		ViewData::$form_data->isHaveDelGrant=FALSE;
		$adminInfo = AdminSession::getAdminSessionInfo ();
		if($adminInfo['AdminName']=='admin'){
			ViewData::$form_data->isHaveDelGrant=TRUE;
		}
		
		BaseAction::loadView ( 'admin/campGradeList' );
	}
	public function campgradeadd() {
		if (! $this->adminGrand['AdminGrant']['manage']) {
			BaseAction::adminError ( "对不起，请不要非法访问" );
		}
		
		require_once APP_LIB_PATH . 'campLib.php';
		ViewData::$form_data = new GradeClass ();
		ViewData::$form_hint = new GradeClass ();
		if ($_POST) {
			$campLib = new CampLib ();
			$campLib->checkGradeForm ();
			if (! BaseAction::isObjectEmpty ( ViewData::$form_hint )) {
				BaseAction::loadView ( "admin/campGradeAdd" );
			}
			try {
				$campLib->addGradeLogic ();
				Header ( "Location:  " . siteUrl ( 'grade/campgradelist' ) . "" );
				exit ();
			} catch ( Exception $e ) {
				ViewData::$form_hint->Message = $e->getMessage ();
			}
		}
		BaseAction::loadView ( 'admin/campGradeAdd' );
	}
	
	/**
	 * 列表
	 */
	public function fulltimegradelist() {
		AdminVisitor::checkGrant ( 'fullTime', FALSE );
		require_once APP_LIB_PATH . 'fullTimeLib.php';
		$fullTimeLib = new FullTimeLib ();
		$Grade = $fullTimeLib->getGradeList ( 1 );
		
		$adminGrand = AdminVisitor::getAdminGrant ();
		if (! isset ( $adminGrand ['fullTime'] )) {
			$fullTimeGrand = array ();
		} else {
			$fullTimeGrand = $adminGrand ['fullTime'];
		}
		
		if (AdminVisitor::checkGrant ( 'manage', FALSE, FALSE )) {
			ViewData::$form_data->Grade = $Grade;
		} else {
			ViewData::$form_data->Grade = $this->getAdminGradeInfo ( $Grade, $fullTimeGrand );
		}
		
	ViewData::$form_data->isHaveDelGrant=FALSE;
		$adminInfo = AdminSession::getAdminSessionInfo ();
		if($adminInfo['AdminName']=='admin'){
			ViewData::$form_data->isHaveDelGrant=TRUE;
		}
		BaseAction::loadView ( 'admin/fullTimeGradeList' );
	}
	
	public function fulltimegradeadd() {
		
		if (! $this->adminGrand['AdminGrant']['manage']) {
			BaseAction::adminError ( "对不起，请不要非法访问" );
		}
		require_once APP_LIB_PATH . 'fullTimeLib.php';
		ViewData::$form_data = new FullTimeGradeClass ();
		ViewData::$form_hint = new FullTimeGradeClass ();
		if ($_POST) {
			$lib = new FullTimeLib ();
			$lib->checkGradeForm ();
			if (! BaseAction::isObjectEmpty ( ViewData::$form_hint )) {
				BaseAction::loadView ( "admin/fullTimeGradeAdd" );
			}
			try {
				$lib->addGradeLogic ();
				BaseAction::adminSuccess ( "添加成功", siteUrl ( "grade/fulltimegradeadd" ), "继续添加" );
				Header ( "Location:  " . siteUrl ( 'grade/fulltimegradelist' ) . "" );
			} catch ( Exception $e ) {
				ViewData::$form_hint->Message = $e->getMessage ();
			}
		}
		BaseAction::loadView ( 'admin/fullTimeGradeAdd' );
	}
	
	/**
	 * 列表
	 */
	public function lotusgradelist() {
		AdminVisitor::checkGrant ( 'lotus', FALSE );
		require_once APP_LIB_PATH . 'lotusLib.php';
		$lib = new lotusLib ();
		$Grade = $lib->getGradeList ( 1 );
		
		$adminGrand = AdminVisitor::getAdminGrant ();
		if (! isset ( $adminGrand ['lotus'] )) {
			$lotusGrand = array ();
		} else {
			$lotusGrand = $adminGrand ['lotus'];
		}
		
		if (AdminVisitor::checkGrant ( 'manage', FALSE, FALSE )) {
			ViewData::$form_data->Grade = $Grade;
		} else {
			ViewData::$form_data->Grade = $this->getAdminGradeInfo ( $Grade, $lotusGrand );
			
		}
		
		ViewData::$form_data->isHaveDelGrant=FALSE;
		$adminInfo = AdminSession::getAdminSessionInfo ();
		if($adminInfo['AdminName']=='admin'){
			ViewData::$form_data->isHaveDelGrant=TRUE;
		}
		BaseAction::loadView ( 'admin/lotusGradeList' );
	}
	public function lotusgradeadd() {
		
		if (! $this->adminGrand['AdminGrant']['manage']) {
			BaseAction::adminError ( "对不起，请不要非法访问" );
		}
		require_once APP_LIB_PATH . 'lotusLib.php';
		ViewData::$form_data = new lotusGradeClass ();
		ViewData::$form_hint = new lotusGradeClass ();
		if ($_POST) {
			$lib = new LotusLib ();
			$lib->checkGradeForm ();
			if (! BaseAction::isObjectEmpty ( ViewData::$form_hint )) {
				BaseAction::loadView ( "admin/lotusGradeAdd" );
			}
			try {
				$lib->addGradeLogic ();
				BaseAction::adminSuccess ( "添加成功", siteUrl ( "grade/lotusgradeadd" ), "继续添加" );
				Header ( "Location:  " . siteUrl ( 'grade/lotusgradelist' ) . "" );
			} catch ( Exception $e ) {
				ViewData::$form_hint->Message = $e->getMessage ();
			}
		}
		BaseAction::loadView ( 'admin/lotusGradeAdd' );
	}
	
	private function getAdminGradeInfo($info, $userGrand) {
		$gradeInfo = array ();
		if ($info) {
			foreach ( $info as $key => $value ) {
				if (in_array ( $value ['GradeId'], $userGrand )) {
					$gradeInfo [] = $value;
				}
			}
		}
		return $gradeInfo;
	}
}